package com.oo.app.ui.main.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.oo.app.R;
import com.oo.app.base.activities.BaseActivity;
import com.oo.app.utils.APKVersionCodeUtils;
import com.oo.app.utils.DataCleanManager;
import com.oo.app.widget.toast.Prompt;
import com.oo.app.widget.toast.SnackbarToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 赵聪 on 2018/7/27.
 * 设置
 */

public class SettingActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.tv_title)
    TextView tv_title;

    @BindView(R.id.switch_msg)
    Switch switch_msg;

    @BindView(R.id.tv_cache_size)
    TextView tv_cache_size; //缓存大小

    @BindView(R.id.tv_version_code)
    TextView tv_version_code; //版本号

    @Override
    protected int getContentView() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        tv_title.setText("设置");
        tv_cache_size.setText(DataCleanManager.getTotalCacheSize(SettingActivity.this)); //计算缓存大小

        tv_version_code.setText("V" + APKVersionCodeUtils.getVerName(SettingActivity.this)); //版本名称

        switch_msg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
//                    mText.setText("开启");
                }else {
//                    mText.setText("关闭");
                }
            }
        });

    }

    @Override
    protected void initData() {
        super.initData();
    }

    public static void show(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }

    @OnClick({R.id.iv_back,R.id.switch_msg,R.id.tv_about_us,R.id.rl_scavenging_caching,R.id.rl_check_update,R.id.tv_praise_and_encouragement,R.id.tv_blacklist,R.id.btn_logout})
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.switch_msg: //消息通知开关

                break;
            case R.id.tv_about_us: //关于我们
                AboutUsActivity.show(SettingActivity.this);
                break;
            case R.id.rl_scavenging_caching: //清除缓存
                cleanCache();
                break;
            case R.id.rl_check_update: //检查更新

                SnackbarToast.make(this, "已是最新版本", 3000l).setPromptThemBackground(Prompt.ERROR).show();

                break;
            case R.id.tv_praise_and_encouragement: //好评鼓励

                break;
            case R.id.tv_blacklist: //黑名单
                BlacklistActivity.show(SettingActivity.this);
                break;
            case R.id.btn_logout:
                finish();
                LoginActivity.show(SettingActivity.this);
                break;

        }
    }
    private void cleanCache() {//清理缓存
        DataCleanManager.clearAllCache(SettingActivity.this);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                tv_cache_size.setText(DataCleanManager.getTotalCacheSize(SettingActivity.this));
                SnackbarToast.make(SettingActivity.this, "清除缓存成功", 3000l).setPromptThemBackground(Prompt.ERROR).show();
            }
        }, 1500);
    }

}
